<?php


namespace app\shop\controller;

use app\admin\controller\ApiCommon;
use app\shop\model\Order as OrderModel;
use app\shop\model\Warehouse as WarehouseModel;
use app\shop\model\Goods as GoodsModel;
use app\crm\model\Customer as CustomerModel;
use Kingdee\Kingdee;
use think\facade\Db;
use think\response\Json;

/**
 * 订单类
 * @package app\shop\controller
 * @author SL
 * @create 2020-06-01
 */
class Order extends ApiCommon
{

    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @author SL
     * @create 2021-06-01
     */
    public function initialize()
    {

        $action = [
            'permission' => [''],
            'allow' => ['export']
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

	/*
	* 修改订单--销售出库单
	* @create 2022-04-19

	*/
	public function updateDetail()
	{
        $this->param['user_id'] = $this->userInfo['id'];
        $this->param['shop_id'] = $this->userInfo['shop_id'];
		$orderModel = new OrderModel();
	    $data       = $orderModel->updateDataDetail($this->param);

	    if (!$data) {

	        return resultArray(['error' => $orderModel->getError()]);

	    }

	    return resultArray(['data' => $data]);
	}

    /**
     * 订单列表
     * @return Json
     * @author SL
     * @create 2021-06-01
     */
    public function index(): Json
    {
        $header = request()->header();
        $this->param['platform'] = $header['platform'];
        $this->param['user_id'] = $this->userInfo['id'];
        $this->param['shopIds'] = $this->getCommonShopIdsCollectByUserType();

        $this->param['person_shop_id'] = $this->userInfo['shop_id'];
        $this->param['admin_user_id'] = $this->userInfo['shop_person_id'];

        $orderModel = new OrderModel();
        $data       = $orderModel->getDataList($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 订单主要详情
     * @return Json
     * @author SL
     * @create 2021-06-02
     */
    public function detail(): Json
    {
        $orderModel = new OrderModel();
        $data       = $orderModel->getDataDetail($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 订单商品详情
     * @return Json
     * @author SL
     * @create 2021-06-02
     */
    public function orderGoodsDetail(): Json
    {
        $orderModel = new OrderModel();
        $data       = $orderModel->getGoodsDataDetail($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 订单商品护理师及服务小项变更详情
     * @return Json
     * @author SL
     * @create 2021-06-02
     */
    public function orderGoodsOptionDetail(): Json
    {
        $orderModel = new OrderModel();
        $data       = $orderModel->orderGoodsOptionDetail($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 创建订单
     * @return Json
     * @author SL
     * @create 2021-06-03
     */
    public function create(): Json
    {

        $this->param['operator_id'] = $this->userInfo['id'];
        $this->param['operator_name'] = $this->userInfo['username'];
        $orderModel = new OrderModel();
        $data       = $orderModel->addData($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);
    }


    /**
     * 修改订单状态
     * @return Json
     * @author SL
     * @create 2021-06-08
     */
    public function checkOrder(): Json
    {
        $orderModel = new OrderModel();
        $this->param['operator_id'] = $this->userInfo['id'];
        $data          = $orderModel->checkOrderData($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data'=> [], 'error' => $orderModel->getError()]);
    }

    /**
     * 锁单
     * @return Json
     * @author SL
     * @create 2021-06-08
     */
    public function lockOrder(): Json
    {
        $orderModel = new OrderModel();
        $this->param['operator_id'] = $this->userInfo['id'];
        $data          = $orderModel->lcokOrderData($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data, 'error' => $orderModel->getError()]);
    }








    /**
     * 订单操作记录
     * @return Json
     * @author SL
     * @create 2021-06-10 9:31
     */
    public function record(): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $orderModel = new orderModel();
        $data          = $orderModel->getOrderRecordList($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }



    /**
     * 销售政策列表
     * @return Json
     * @author LIU
     * @create 2021-10-27 9:31
     */
    public function saleList(): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $orderModel = new orderModel();
        $data          = $orderModel->getSaleList();
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }


    /**
     * 仓库列表
     * @return Json
     * @author LIU
     * @create 2021-10-27 9:31
     */
    public function houseList(): Json
    {
        $orderModel = new orderModel();
        $data          = $orderModel->getHouseList($this->param);
        if (!$data) {
            return resultArray(['error' => $orderModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }







    /**
     * 订单退款单
     *
     */
    public function refundMoney()
    {
        $order_id = $this->param['id'];
        $kingDee = new Kingdee();
        $res = $kingDee->refund_money($order_id);
    }


    /**
     * 订单退货单
     *
     */
    public function refundGoods()
    {
        $order_id = $this->param['id'];
        $kingDee = new Kingdee();
        $res = $kingDee->refund_goods($order_id);
    }


    /**
     * 推送会员
     *
     */
    public function pushCustomer()
    {
        $user_id = $this->param['id'];
        $kingDee = new Kingdee();
        $res = $kingDee->add_customer($user_id);//保存
        //$res = $kingDee->submit_customer($user_id);//提交
        //$res = $kingDee->audit_customer($user_id);//审核
        //$res = $kingDee->again_audit_customer($user_id);//反审核
    }


    /**
     * 收款单
     *
     */
    public function receiptMoney()
    {
        $order_id = $this->param['id'];
        $kingDee = new Kingdee();
        $res = $kingDee->receipt_money($order_id);
    }


    /**
     * 导出订单
     * @author Mr.Liu
     * @create 2021-06-09 10:46
     */
    public function export()
    {
        $requestId = isset($this->request['id']) && $this->request['id'] ? $this->request['id'] : [];
        if($requestId){
            $idArr = is_array($requestId) ? $requestId : explode(',', $requestId);
        }else{
            $idArr = [];
        }
        $orderModel = new orderModel();
        $data          = $orderModel->getDataList(['page' => 1,'limit' => 10000,'id' => $idArr]);

        if(empty($data['list'])){
            $data['list'] = [];
        }
        $callName = ['deal_time' =>'成交日期','consignee' =>'客户名称','king_no' =>'客户K3代码','shop_name' => '门店','sale_name' => '护理师','goods_price' => '标准金额','order_amount' => '销售金额','business_type' => '业务类型','order_type' => '订单类型'];
        $downUrl = exportExcelData('order',$callName,$data['list'],'xlsx',1);
        return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);
    }





    /**
     * 订单下推销售出库单
     *
     */
    public function orderPush()
    {
        $order_id = $this->param['id'];
        $user_id  = $this->userInfo['id'];
        // 查询订单信息 判断订单的支付状态
        $orderModel = new orderModel();
        $orderInfo = $orderModel->where('id', $order_id)->find();
        if ( !$orderInfo ) {
            return resultArray(['error'=> '订单信息获取失败']);
        }

        // 判断订单是否已下推
        if ($orderInfo['status'] >= 2) {
            return resultArray(['error'=> '该订单已下推']);
        }
        $customer= Db::name('customer')->where('id',$orderInfo['uid'])->field('king_no,king_status')->find();

        if (!$customer['king_no'] || !$customer['king_status'] == 2)
        {
            exit(json_encode(['code'=>404, 'data' => [], 'msg'=>'客户信息没有金蝶编码或者是禁用状态']));
        }

        $kingDee = new Kingdee();
        $res = $kingDee->send_order($order_id);
        if ($res['code']) {
            return resultArray(['error'=> $res['msg']]);
        }
        $orderModel = new OrderModel();
        $orderModel->addActionRecord($user_id, 'shop_order_check', $order_id, '订单已下推', 'shop', 'order');
        return resultArray(['data'=> [], 'error'=> '操作成功']);
    }

    /**
     * 获取销售订单数据
     *
     */
    public function getSaleOrder()
    {
        $order_id = $this->param['order_id'];
        $user_id  = $this->userInfo['id'];
        // 查询订单信息 判断订单的支付状态
        $orderModel = new orderModel();
        $orderInfo = $orderModel->where('id', $order_id)->find();
        if ( !$orderInfo ) {
            return resultArray(['error'=> '订单信息获取失败']);
        }

        // 判断客户是否确认订单
        if ($orderInfo['king_status'] != 1) {
            return resultArray(['error'=> '金蝶推送状态异常']);
        }

        // 金蝶销售出库单号
        if ( !$orderInfo['king_sale_no'] ) {
            return resultArray(['error'=> '金蝶销售出库单号为空']);
        }

        $kingDee = new Kingdee();
        $data = $kingDee->get_sale_order($orderInfo['king_sale_no']);
        return resultArray(['data'=> $data]);
    }
}

?>
